
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.event.*;


public class GUI extends JFrame{

	private JButton add, delete, addText, create, load, saveAs, print;
	private JList dList;
	private JCheckBox pWords;
	private JLabel pType, eWord;
	private JComboBox pChoice;
	private JTextField word;
	private JTextField[][] theBoard;
	private JPanel display;
	private WordList wList;
	private JScrollPane list;
	private int[] array;
	private PuzzleGenerator pG;
	private PuzzleBoard board;
	private FileHandler fL;
	
	/**
	 * creates new GUI object
	 */
	public GUI(){
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Puzzle Creator");
		setPreferredSize(new Dimension(1200,800));
		setVisible(true);
		setResizable(false);
		this.addWindowListener(new Painter());
		
		wList = new WordList();
		board = new PuzzleBoard(10,10);
		add = new JButton("Add Word");
		fL = new FileHandler(board);
		delete = new JButton("Delete Word");
		addText = new JButton("Add Text File");
		create = new JButton("Create Puzzle");
		load = new JButton("Load Puzzle");
		saveAs = new JButton("Save Puzzle As");
		print = new JButton("Print Puzzle");
		dList = new JList(wList.getWords());
		pWords = new JCheckBox("Show Puzzle Words");
		eWord = new JLabel("Enter Word:");
		pType = new JLabel("        Puzzle Type:");
		pChoice = new JComboBox();
		theBoard = new JTextField[30][30];
		word = new JTextField(30);
		list = new JScrollPane(dList);
		display = new JPanel();
		display.setBackground(new Color(115,137,249));
		print.setEnabled(false);
		pChoice.addItem("WordSearch");
		pChoice.addItem("Crossword");
		pChoice.addItem("Other");
		dList.setBorder( new LineBorder(Color.BLACK));
		list.setPreferredSize(new Dimension(200,400));
		display.setPreferredSize(new Dimension(1200,800));
		display.setVisible(true);
		word.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		
		makeBoard();
		addToFrame();
		setComponentSize();
		setComponentLocations();
		addListeners();
		
		
	}
	
	/**
	 * adds all the components to the frame
	 */
	
	private void addToFrame(){
		
		add(display);
		displayboard();
		display.add(add);
		display.add(delete);
		display.add(addText);
		display.add(create);
		display.add(load);
		display.add(saveAs);
		display.add(print);
		display.add(list);
		display.add(pWords);
		display.add(eWord);
		display.add(pType);
		display.add(pChoice);
		display.add(word);
		
		pack();
		
		
	}
	
	private void setComponentSize(){
		
		add.setSize(130,30);
		delete.setSize(130,30);
		addText.setSize(130,30);
		load.setSize(130,30);
		create.setSize(130,30);
		saveAs.setSize(130,30);
		print.setSize(130,30);
		word.setSize(128, 22);
		pType.setSize(150,20);
		
	}
	
	/**
	 * sets the locations of the components in the panel
	 */
	
	private void setComponentLocations(){
		
		setBoard();
		add.setLocation(250, 80);
		delete.setLocation(250, 120);
		addText.setLocation(250, 160);
		create.setLocation(250, 250);
		load.setLocation(250, 290);
		saveAs.setLocation(250, 330);
		print.setLocation(250, 370);
		list.setLocation(10, 80);
		pWords.setLocation(10, 560);
		eWord.setLocation(10, 15);
		pType.setLocation(10, 530);
		pChoice.setLocation(108, 528);
		word.setLocation(80, 10);
		
		
	}
	
	private void addListeners(){
		
		add.addActionListener(new buttonListener());
		delete.addActionListener(new buttonListener());
		addText.addActionListener(new buttonListener());
		create.addActionListener(new buttonListener());
		load.addActionListener(new buttonListener());
		saveAs.addActionListener(new buttonListener());
		print.addActionListener(new buttonListener());
		word.addActionListener(new buttonListener());
		pWords.addActionListener(new buttonListener());
		word.addKeyListener(new key());
		
	}
	
	/**
	 * handles the look of the board
	 */
	
	private void makeBoard(){
		
		for(int x = 0; x < theBoard.length; x++){
			
			for(int y = 0; y< theBoard[x].length; y++){
				
				theBoard[x][y] = new JTextField(2);
				
			}
			
		}
		
	}
	
	private void displayboard(){
		
		for(int x = 0; x < theBoard.length; x++){
			
			for(int y = 0; y< theBoard[x].length; y++){
				
				theBoard[x][y].setFont(new Font("Times New Roman",Font.PLAIN,16));
				theBoard[x][y].setHorizontalAlignment(JTextField.CENTER);
				theBoard[x][y].setEditable(false);
				theBoard[x][y].setVisible(false);
				display.add(theBoard[x][y]);
				
			}
			
		}
		
	}
	
	/**
	 * sets up the display of the board
	 */
	private void setBoard(){
		
		for(int x = 0; x < theBoard.length; x++){
			
			for(int y = 0; y< theBoard[x].length; y++){
				
				theBoard[x][y].setLocation((400+(x*26)), (80+(y*21)));
				
			}
			
		}
		
	}
	
	private void loadBoard(int rows, int cols){
		
		
		for(int x = 0; x < rows; x++){
			
			for(int y = 0; y < cols; y++){
				
				theBoard[x][y].setVisible(true);
				theBoard[x][y].setText(Character.toString(board.getChar(rows, cols)));
			
			}
		}
		
	}

	private class buttonListener implements ActionListener{
	
		/**
		 * handles all events of the user interface
		 * @param e and event from the interface
		 */	
		public void actionPerformed(ActionEvent e){
			
			fileOperator fO;
			
			if( e.getSource() == add){
				
				addWord();
				
			}
			if( e.getSource() == delete){
				
				int[] array = new int[dList.getSelectedIndices().length*2];
				array = dList.getSelectedIndices();
				
				for(int x = (dList.getSelectedIndices().length-1); x >= 0; x--){
					
					wList.removeWord(array[x]);
					
				}
				
				dList.clearSelection();
				dList.setListData(wList.getWords());
				
			}
			else if( e.getSource() == addText){
					
				fO = new fileOperator("text", fL, wList);
				dList.clearSelection();
				dList.setListData(wList.getWords());
				
			}
			else if(e.getSource() == create){
				
				if(pChoice.getSelectedItem() == "WordSearch"){
					pG = new PuzzleGenerator(board, wList);
					pG.createPuzzle();
					loadBoard(board.getWidth(), board.getHeight());
					System.out.println(board.getWidth() + "\n" + board.getHeight());
					print.setEnabled(true);
					
				}
				else{JOptionPane.showInputDialog(null, "Option not available.");}
				
			}
			else if(e.getSource() == load){
				
				fO = new fileOperator("load", fL, wList);
				loadBoard(17,25);
				
				
			}
			else if(e.getSource() == saveAs){
				
				fO = new fileOperator("save", fL, wList);
				
			}
			else if(e.getSource() == print){
				
				fL.printPuzzle();
				
			}
			else if(e.getSource()== pWords){
				
				if(pWords.isSelected()){
					
					//showWords();
					
				}
				else{
					
					//normalizePuzzle();
					
				}
				
				
			}
			
		}
		
	}
	
	private class Painter extends WindowAdapter{
		
		public void windowActivated(WindowEvent e){
			
			setComponentLocations();
			setComponentSize();
			
		} 

		public void windowDeiconified(WindowEvent e) {
			
			setComponentLocations();
			setComponentSize();
			
		}
        
		public void windowIconified(WindowEvent e) {
			
			setComponentLocations();
			setComponentSize();
			
		}

		public void windowStateChanged(WindowEvent e) {
			
			setComponentLocations();
			setComponentSize();
			
		} 

		
		
	}
	
	private class key extends KeyAdapter{
		
		public void keyReleased(KeyEvent ke){
			
			if(ke.getKeyCode() == KeyEvent.VK_ENTER){
				
				addWord();
				
			}
			
		}
		
	}
	
	private void addWord(){
		
		if(word.getText().equals("") || word.getText().equals("")){
			
			JOptionPane.showMessageDialog(null, "Please enter a word.");
			
		}
		else{
			
			try{
				
				wList.addWord(word.getText());
				
			}catch(Exception e){
				
				JOptionPane.showMessageDialog(null, e.getMessage());
				
			}
			
		}
		
		dList.setListData(wList.getWords());
		word.setText("");
		
	}
	
}
